package com.zodiac.app.datathinking.szse.b0602;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.zodiac.app.datathinking.szse.Extractors;
import com.zodiac.app.datathinking.szse.Processor;
import lombok.Data;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Extractors(B0602Extractor.class)
@Component
public class B0602Processor implements Processor<List<Map>, List<B0602Processor.Response>> {
    private final Pattern reportYearPattern = Pattern.compile("[\\d]{4}年");

    @Override
    public List<Response> process(List<Map> request) {
        Preconditions.checkNotNull(request);
        return request.stream().map(item -> {
            Response response = new Response();
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(item));
            response.setStockCode(jsonObject.getJSONArray("secCode").getString(0));
            response.setStockName(jsonObject.getJSONArray("secName").getString(0));
            response.setPublishTime(jsonObject.getString("publishTime"));
            response.setReportTitle(jsonObject.getString("title"));
            response.setReportPath(jsonObject.getString("attachPath"));

            final String reportTitle = response.getReportTitle();
            response.setReportYear(matchReportYear(reportTitle));

            final B0602Extractor.ReportType reportType = matchReportType(reportTitle);
            if (Objects.nonNull(reportType)) {
                response.setReportType(reportType.getKey());
            }

            response.setReportFormally(Stream.of("摘要", "正文").noneMatch(reportTitle::contains));

            return response;
        }).collect(Collectors.toList());
    }

    /**
     * 匹配报告年份
     *
     * @param reportTitle
     * @return
     */
    private String matchReportYear(final String reportTitle) {
        Matcher matcher = reportYearPattern.matcher(reportTitle);
        if (matcher.find()) {
            return matcher.group().substring(0, 4);
        }
        return null;
    }

    /**
     * 匹配报告类型
     *
     * @param reportTitle
     * @return
     */
    private B0602Extractor.ReportType matchReportType(final String reportTitle) {
        return EnumUtils.getEnumList(B0602Extractor.ReportType.class).stream().filter(item -> reportTitle.contains(item.getName())).findFirst().orElse(null);
    }

    @Data
    public static class Response {
        // 股票代码
        private String stockCode;
        // 股票简称
        private String stockName;
        // 发布时间
        private String publishTime;
        // 报告标题
        private String reportTitle;
        // 下载路径
        private String reportPath;
        // 报告年份
        private String reportYear;
        // 报告类型
        private String reportType;
        // 是否为正式报告
        private boolean reportFormally;
    }
}
